//
//  MYViewController.m
//  investevent
//
//  Created by jiangshao on 16/3/20.
//  Copyright © 2016年 com.scj.investevent. All rights reserved.
//

#import "MYViewController.h"
#import "SettingModel.h"
#import "SettingCell.h"
#import "LuckyQueryViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import <SVProgressHUD.h>
#import "LoginViewController.h"

NSString * const SettingCellId=@"SettingCell";

@implementation MYViewController{
    UITableView *_bkTableView;
    NSMutableArray *datas;
    UILabel *phoneLabel;
    UIButton *loginOutButton;
    UIView *headView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _bkTableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:_bkTableView];
    
    
    datas=[self initdatas];
    
//    AVUser *user = [AVUser user];// 新建 AVUser 对象实例
//    user.username = @"13665073611";// 设置用户名
//    user.password =  @"123456";// 设置密码
//    user.email = @"tom@leancloud.cn";// 设置邮箱
    
//    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
//        if (succeeded) {
//            // 注册成功
//        } else {
//            // 失败的原因可能有多种，常见的是用户名已经存在。
//        }
//    }];

//    [AVUser logInWithUsernameInBackground:@"13665073612" password:@"123456" block:^(AVUser *user, NSError *error) {
//        if (user != nil) {
//
//        } else {
//            [SVProgressHUD showErrorWithStatus:[error description]];
//        }
//    }];
    NSInteger top=64;
    if(kDevice_Is_iPhoneX){
        top=top+24;
    }
    _bkTableView.contentInset=UIEdgeInsetsMake(top, 0, 0, 0);
    [_bkTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    _bkTableView.scrollEnabled=NO;
    [_bkTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    _bkTableView.dataSource  = self;
    _bkTableView.delegate    = self;
    
    
    [_bkTableView registerNib:[UINib nibWithNibName:SettingCellId bundle:nil] forCellReuseIdentifier:SettingCellId];
    _bkTableView.tableHeaderView=[self addHeadView];

    loginOutButton=[UIButton new];
    [self.view addSubview:loginOutButton];
    [loginOutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(320);
        make.leading.equalTo(self.view).offset(30);
        make.trailing.equalTo(self.view).offset(-30);
        make.height.equalTo(@45);
    }];
    loginOutButton.hidden=YES;
    [loginOutButton addTarget:self action:@selector(loginOut) forControlEvents:UIControlEventTouchUpInside];
    [loginOutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginOutButton setTitle:@"登 出" forState:UIControlStateNormal];
    loginOutButton.backgroundColor=themeColor;
//    loginOutButton.layer.masksToBounds=YES;
//    loginOutButton.layer.cornerRadius=8;
}

-(UIView *)addHeadView{
    headView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, screenviewwidth, 100)];
    UIImageView *userimage=[UIImageView new];
    [headView addSubview:userimage];
    [userimage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(headView).offset(10);
        make.centerY.equalTo(headView);
        make.width.equalTo(@40);
        make.height.equalTo(@40);
    }];
    userimage.image=[UIImage imageNamed:@"userdefault"];
    phoneLabel=[UILabel new];
    [headView addSubview:phoneLabel];
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(userimage);
        make.leading.equalTo(userimage.mas_trailing).offset(10);
    }];
    headView.backgroundColor=RGBCOLOR(255, 193, 116);
//    phoneLabel.text=@"15659777777";
    phoneLabel.textColor=[UIColor whiteColor];
    UITapGestureRecognizer *getsture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(login)];
    [headView addGestureRecognizer:getsture];
    return headView;
}

- (void)getUser{
    AVUser *currentUser = [AVUser currentUser];
    if (currentUser != nil) {
        // 跳转到首页
        phoneLabel.text=currentUser.username;
        loginOutButton.hidden=NO;
    } else {
        //缓存用户对象为空时，可打开用户注册界面…
        phoneLabel.text=@"注册/登陆";
        loginOutButton.hidden=YES;
    }
}

-(void)loginOut{
    [AVUser logOut];
    UIStoryboard *jump=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    LoginViewController *loginViewController=[jump instantiateViewControllerWithIdentifier:@"LoginViewController"];
    loginViewController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:loginViewController animated:YES];
}


-(void)login{
    AVUser *currentUser = [AVUser currentUser];
    if (currentUser != nil) {
        return;
    }
    UIStoryboard *jump=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    LoginViewController *loginViewController=[jump instantiateViewControllerWithIdentifier:@"LoginViewController"];
    loginViewController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:loginViewController animated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [[NavService getNav] setNavigationBarHidden:NO];
    self.tabBarController.navigationItem.title=@"我的";
    self.tabBarController.navigationItem.rightBarButtonItem=nil;
    [self getUser];
}


-(NSMutableArray *)initdatas{
    NSMutableArray *fortunedatas=[[NSMutableArray alloc]init];
    
    NSMutableArray *section;
   
        section=[[NSMutableArray alloc]init];
        SettingModel *setting=[[SettingModel alloc]init];
        setting.title=@"中签查询";
        [fortunedatas addObject:setting];

        setting=[[SettingModel alloc]init];
        setting.title=@"关于我们";
        [fortunedatas addObject:setting];

    return fortunedatas;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





#pragma mark - UITableView delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    static NSString * cellIdentifier = SettingCellId;
    //
    SettingCell *cell = [tableView dequeueReusableCellWithIdentifier:SettingCellId];
    
//    NSMutableArray *datas=fortunedatas[indexPath.section];
    SettingModel *settingModel=datas[indexPath.row];
    cell.indexPath=indexPath;
    cell.datas=datas;
    [cell setContent:settingModel];
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 15;
//}
//
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 80)];
//    [sectionView initBackgroundColor:@"colorScheme_answersheet_result_4"];
//    if(section == 0)
//    {
//        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 1, self.width, 0.5f)];
//        [lineView initBackgroundColor:@"colorScheme_line"];
//        [sectionView addSubview:lineView];
//    }
//    return sectionView;
//}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return datas.count;
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSInteger row=indexPath.row;
    switch (row) {
        case 0:{
            AVUser *currentUser = [AVUser currentUser];
            if (currentUser == nil) {
                [self login];
                return;
            }
            LuckyQueryViewController *lucky=[[LuckyQueryViewController alloc]init];
            lucky.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:lucky animated:YES];
            break;
        }
        case 1:{
            UIStoryboard *jump=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
            UIViewController *myversion=[jump instantiateViewControllerWithIdentifier:@"myversion"];
            myversion.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:myversion animated:YES];
            break;
        }
            
        default:
            break;
    }
    
}


@end
